package workbench.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.DateUtil;
import util.UUIDUtil;
import workbench.dao.*;
import workbench.domain.*;
import workbench.exception.ConvertException;
import workbench.exception.DeleteException;
import workbench.exception.InsertClueException;
import workbench.service.ClueService;
import java.util.Date;
import java.util.List;

@Service
public class ClueServiceImpl implements ClueService {
    @Autowired
    private ClueDao clueDao;
    @Autowired
    private ActivityDao activityDao;
    @Autowired
    private ClueActivityRelationDao clueActivityRelationDao;
    @Autowired
    private ClueRemarkDao clueRemarkDao;
    @Autowired
    private CustomerDao customerDao;
    @Autowired
    private CustomerRemarkDao customerRemarkDao;
    @Autowired
    private ContactsDao contactsDao;
    @Autowired
    private ContactsRemarkDao contactsRemarkDao;
    @Autowired
    private ContactsActivityRelationDao contactsActivityRelationDao;
    @Autowired
    private TranDao tranDao;
    @Autowired
    private TranHistoryDao tranHistoryDao;
    @Transactional
    public void addClue(Clue clue) throws InsertClueException {
        int count=clueDao.addClue(clue);
        if(count!=1){
            throw new InsertClueException("增加线索失败");
        }
    }
    public Clue getClueById(String id) throws Exception {
        Clue clue=clueDao.getClueById(id);
        if(clue==null){
            throw new Exception("未查询到该用户信息");
        }
        return clue;
    }
    public List<Activity> getActivityById(String id){
        List list=activityDao.getActivityByClueId(id);
        return list;
    }
    @Transactional
    public void unbind(String aId,String cId) throws DeleteException {
        int count = clueDao.unbind(aId, cId);
        if(count==0){
            throw new DeleteException("删除市场活动和线索的关联失败");
        }
    }
    public List<Activity> getNotBindActivityListByName(String aname,String clueId){
        List<Activity> list=activityDao.getNotBindActivityListByName(aname,clueId);
        return list;
    }
    @Transactional
    public void bind(String[] aIds,String cId) throws Exception {
        int count=0;
        for(String s:aIds){
            count+=clueActivityRelationDao.bind(UUIDUtil.getUUID(),cId,s);
        }
        if(count!=aIds.length){
            throw new Exception("插入操作异常");
        }
    }
    public List<Activity> getActivityListByName(String aname){
        List<Activity> list=activityDao.getActivityListByName(aname);
        return list;
    }
    @Transactional
    public void convert(boolean flag, String clueId, Tran tran,String createBy) throws ConvertException {
        Clue clue = clueDao.getById(clueId);
        String company = clue.getCompany();
        Customer customer = customerDao.getCustomerByCompanyName(company);
        if (customer == null) {
            customer = new Customer();
            customer.setId(UUIDUtil.getUUID());
            customer.setAddress(clue.getAddress());
            customer.setWebsite(clue.getWebsite());
            customer.setPhone(clue.getPhone());
            customer.setOwner(clue.getOwner());
            customer.setNextContactTime(clue.getNextContactTime());
            customer.setName(company);
            customer.setDescription(clue.getDescription());
            customer.setCreateTime(DateUtil.dateToString19Char(new Date(System.currentTimeMillis())));
            customer.setCreateBy(clue.getCreateBy());
            customer.setContactSummary(clue.getContactSummary());
            int count = customerDao.save(customer);
            if (count != 1) {
                throw new ConvertException("转换失败，保存客户信息时出现异常");
            }
        }
        Contacts contacts = new Contacts();
        contacts.setId(UUIDUtil.getUUID());
        contacts.setSource(clue.getSource());
        contacts.setOwner(clue.getOwner());
        contacts.setNextContactTime(clue.getNextContactTime());
        contacts.setMphone(clue.getMphone());
        contacts.setJob(clue.getJob());
        contacts.setFullname(clue.getFullname());
        contacts.setEmail(clue.getEmail());
        contacts.setDescription(clue.getDescription());
        contacts.setCustomerId(customer.getId());
        contacts.setCreateTime(DateUtil.dateToString19Char(new Date(System.currentTimeMillis())));
        contacts.setCreateBy(createBy);
        contacts.setContactSummary(clue.getContactSummary());
        contacts.setAppellation(clue.getAppellation());
        contacts.setAddress(clue.getAddress());
        int count = contactsDao.save(contacts);
        if (count != 1) {
            throw new ConvertException("转换失败，添加客户信息时失败");
        }
        List<ClueRemark> clueRemarks=clueRemarkDao.getClueRemarksByClueId(clueId);
        for(ClueRemark c:clueRemarks){
            String noteContent=c.getNoteContent();
            CustomerRemark customerRemark=new CustomerRemark();
            customerRemark.setNoteContent(noteContent);
            customerRemark.setId(UUIDUtil.getUUID());
            customerRemark.setCustomerId(customer.getId());
            customerRemark.setCreateTime(DateUtil.dateToString19Char(new Date(System.currentTimeMillis())));
            customerRemark.setCreateBy(createBy);
            customerRemark.setEditFlag("0");
            count =customerRemarkDao.save(customerRemark);
            if(count!=1){
                throw new ConvertException("转换失败，客户备注信息保存异常");
            }
            ContactsRemark contactsRemark=new ContactsRemark();
            contactsRemark.setNoteContent(noteContent);
            contactsRemark.setId(UUIDUtil.getUUID());
            contactsRemark.setContactsId(contacts.getId());
            contactsRemark.setCreateTime(DateUtil.dateToString19Char(new Date(System.currentTimeMillis())));
            contactsRemark.setCreateBy(createBy);
            contactsRemark.setEditFlag("0");
            count =contactsRemarkDao.save(contactsRemark);
            if(count!=1){
                throw new ConvertException("转换失败，客户备注信息保存异常");
            }
        }
        List<ClueActivityRelation> list=clueActivityRelationDao.getListByClueId(clueId);
        for(ClueActivityRelation clueActivityRelation:list){
            String activityId=clueActivityRelation.getActivityId();
            ContactsActivityRelation contactsActivityRelation=new ContactsActivityRelation();
            contactsActivityRelation.setId(UUIDUtil.getUUID());
            contactsActivityRelation.setActivityId(activityId);
            contactsActivityRelation.setContactsId(contacts.getId());
            contactsActivityRelationDao.save(contactsActivityRelation);
        }
        if(flag){
            tran.setId(UUIDUtil.getUUID());
            tran.setCreateBy(createBy);
            tran.setCreateTime(DateUtil.dateToString19Char(new Date(System.currentTimeMillis())));
            tran.setContactsId(contacts.getId());
            tran.setSource(contacts.getSource());
            tran.setOwner(contacts.getOwner());
            tran.setNextContactTime(contacts.getNextContactTime());
            tran.setCustomerId(customer.getId());
            tran.setContactSummary(contacts.getContactSummary());
            tran.setDescription(contacts.getDescription());
            count=tranDao.save(tran);
            if(count!=1){
                throw new ConvertException("转换失败创建交易信息失败");
            }
            TranHistory tranHistory=new TranHistory();
            tranHistory.setId(UUIDUtil.getUUID());
            tranHistory.setTranId(tran.getId());
            tranHistory.setCreateBy(createBy);
            tranHistory.setCreateTime(DateUtil.dateToString19Char(new Date(System.currentTimeMillis())));
            tranHistory.setExpectedDate(tran.getExpectedDate());
            tranHistory.setMoney(tran.getMoney());
            tranHistory.setStage(tran.getStage());
            count=tranHistoryDao.save(tranHistory);
        }
        for(ClueRemark clueRemark:clueRemarks){
            count=clueRemarkDao.delete(clueRemark);
            if(count!=1){
                throw new ConvertException("转换失败，删除线索备注信息失败");
            }
        }
        for(ClueActivityRelation clueActivityRelation:list){
            count=clueActivityRelationDao.delete(clueActivityRelation);
            if(count!=1){
                throw new ConvertException("转换失败，删除线索关联信息失败");
            }
        }
        count=clueDao.deleteByClueId(clueId);
        if(count!=1){
            throw new ConvertException("转换失败，删除线索信息失败");
        }
    }
}
